<template>
	<view>
		<view class="address_top">
			<uni-icons type="info" color="#FF0000" size="16"></uni-icons>
			仅限浙江省台州市温岭市地区派送
		</view>

		<view class="items" v-for="(item,index) in addList" :key="index" @click="goback(item)">
			<view class="items_1 flex">
				<view class="t1">{{item.userName}} {{item.userPhone}}</view>
				<view class="tip" v-if="item.flag == 1">默认</view>
			</view>
			<view class="text">
				{{item.province}}{{item.city}}{{item.county}} {{item.address}}
			</view>
			<view class="flex_jc_sb">
				<view class="icon_l flex" @click.stop="changeFlag(item)">
					<image v-if="item.flag == 1" src="/static/radioact.png" mode=""></image>
					<image v-else src="/static/radio.png" mode=""></image>
					默认
				</view>
				<view class="flex">
					<view class="iconss" @click.stop="editAddress(item)">
						<uni-icons type="compose" size="22" color="#D1D1D6"></uni-icons>
					</view>
					<view class="iconss" @click.stop="removeAddress(item)">
						<uni-icons type="trash" size="22" color="#D1D1D6"></uni-icons>
					</view>
				</view>
			</view>
		</view>
		<view class="k1" v-if="addList.length==0">
			<image src="/static/dkong.png" mode=""></image>
			<view class="texts">
				暂无收货地址
			</view>
		</view>

		<view class="" style="height: 220rpx;"></view>
		<view class="footerbtn">
			<view class="cont">
				<view class="btn" @click="handleSubmit">
					增加收货地址
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		reactive,
		ref
	} from 'vue'
	import {
		onShow,
		onLoad
	} from '@dcloudio/uni-app'
	import {
		addressList,
		addressRemove,
		addressEdit
	} from '@/common/http.api.js'

	const type = ref(0)
	onLoad((option) => {
		type.value = option.type
	})

	function goback(item) {
		if (type.value == 1) {
			uni.$emit('address', item)
			uni.navigateBack()
		}
	}
	const addList = ref([])
	onShow(() => {
		getlist()
	})

	function getlist() {
		addressList({}).then(res => {
			addList.value = res.data
		})
	}
	//修改默认地址
	function changeFlag(item) {
		if (item.flag == 0) {
			item.flag = 1
		} else {
			item.flag = 0
		}
		addressEdit(item).then(res => {
			if (res.code == 200) {
				uni.showToast({
					title: '已设置',
					icon: 'none'
				})
				getlist()
			}
		})
	}
	//删除
	function removeAddress(item) {
		uni.showModal({
			title: '系统提示',
			content: '确定要删除该地址嘛？！',
			confirmText: '确定',
			confirmColor: '#FF7700',
			success: (res) => {
				if (res.confirm) {
					addressRemove({
						ids: [item.id]
					}).then(res => {
						if (res.code == 200) {
							uni.showToast({
								title: '地址已删除',
								icon: 'none'
							})
							getlist()
						}
					})
				} else if (res.cancel) {}
			}
		});
	}
	//修改
	function editAddress(item) {
		uni.navigateTo({
			url: `/pages/user/setaddress?type=2&id=${item.id}`
		})
	}

	function handleSubmit() {
		uni.navigateTo({
			url: '/pages/user/setaddress?type=1'
		})
	}
</script>

<style lang="scss">
	page {
		background: #F8F8F8;
	}

	.k1 {
		text-align: center;
		margin: 200rpx auto;
		width: 320rpx;

		image {
			width: 320rpx;
			height: 300rpx;
		}

		.texts {
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 28rpx;
			color: #979797;
			margin-top: 10rpx;
		}
	}

	.address_top {
		width: 750rpx;
		height: 72rpx;
		background: #FFF3F8;
		border-radius: 0rpx 0rpx 0rpx 0rpx;
		position: sticky;
		top: 0%;
		padding: 18rpx 20rpx;
		box-sizing: border-box;
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		font-size: 26rpx;
		color: #FF0000;
	}

	.items {
		width: 710rpx;
		background: #FFFFFF;
		border-radius: 20rpx 20rpx 20rpx 20rpx;
		margin: 20rpx auto;
		padding: 30rpx;
		box-sizing: border-box;

		.t1 {
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			font-size: 36rpx;
			color: #000000;
		}

		.tip {
			width: 76rpx;
			height: 36rpx;
			background: #FFF1E5;
			border-radius: 4rpx 4rpx 4rpx 4rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 24rpx;
			color: #FF7700;
			line-height: 36rpx;
			text-align: center;
			margin-left: 30rpx;
		}

		.text {
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 24rpx;
			color: #000000;
			margin: 16rpx auto 30rpx;
		}

		.icon_l {
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 26rpx;
			color: #979797;

			image {
				width: 40rpx;
				height: 40rpx;
				margin-right: 20rpx;
			}
		}

		.iconss {
			margin-left: 15rpx;
		}
	}


	.footerbtn {
		width: 750rpx;
		background: #FFFFFF;
		position: fixed;
		bottom: 0%;
		height: 120rpx;
		box-shadow: 0rpx 1rpx 10rpx 0rpx rgba(0, 0, 0, 0.2);
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);

		.cont {
			display: flex;
			height: 120rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 32rpx;
			align-items: center;
			padding: 20rpx 20rpx 20rpx 40rpx;
			box-sizing: border-box;
			justify-content: space-between;

			.btn {
				width: 670rpx;
				height: 80rpx;
				background: #FF7700;
				border-radius: 12rpx 12rpx 12rpx 12rpx;
				font-family: PingFang SC, PingFang SC;
				border-radius: 40rpx;
				font-weight: 500;
				font-size: 28rpx;
				color: #ffffff;
				line-height: 80rpx;
				text-align: center;
			}
		}
	}
</style>